package com.bigdata.demo.controller;

import com.bigdata.demo.entity.image;
import com.bigdata.demo.methods.Public;
import com.bigdata.demo.methods.Connection_BigData;
import org.springframework.web.bind.annotation.*;
import com.bigdata.demo.entity.Container;
import com.bigdata.demo.methods.Connection_BigData.*;

import java.util.Iterator;
import java.util.Objects;

import static com.bigdata.demo.methods.Connection_BigData.opList;
import static com.bigdata.demo.methods.Connection_BigData.optimizeSchemeList;

@RestController //用来表示这是一个对外的接口
//@CrossOrigin(originPatterns = "*", allowCredentials = "true", allowedHeaders = "*", methods = {})//跨域设置
//可以接受任何主机对本地的访问
@CrossOrigin("*")
public class image_Delete {
    //这是大数据平台接口设计——镜像删除——image_delete
    @GetMapping ("/image_del")
    public String create_docker(
            //接收到的参数
            @RequestParam("imageid") String imageid, //容器名
            @RequestParam("imagetime") String imagetime,
            @RequestParam("imageoperation") String imageoperation,
            @RequestParam("imagestatus") String imagestatus,
            @RequestParam("imagecompose")  String imagecompose,
            @RequestParam("imageexample")  String imageexample

    ){

        System.out.println("imageid:"+imageid);


//        String hostname = "125.221.232.243";//要连接主机的ip
//        String username = "root";//登录的用户名
//        String password = "123@bigdata";  //用户密码
//        String pri; //接口的返回，运行状态
//
//        //调用封装的类Connection_BigData，远程连接集群
//        Connection_BigData con = new Connection_BigData(hostname, username, password);
//        String path = Public.SHELL_dir;   //脚本所在目录
//        String cmd="cd "+path+"; ";                   //找到脚本所在目录
//
//        //拼接命令，例如：/bin/bash container_delete.sh nginx dev ，没使用container_volume
//        String shell_name = "image_delete.sh ";             //脚本名
//        //每个参数后面接空格，这里表示将这些参数传给脚本
//        shell_name += imageid +' ';
//
//
//        cmd += "/bin/bash "+shell_name;              //脚本执行的命令
//
//        System.out.println("cmd:"+cmd);  //可以打印出来，看命令是否正确
//        pri = con.connection(cmd); //调用函数connection，连接集群，并执行cmd命令，返回运行状态
//        optimizeSchemeList.remove(container_name);

//        Iterator<image> iter = opList.iterator();
//
//        while (iter.hasNext()==true){
//            System.out.println(iter.next().getImageid());
//            if (iter.next().getImageid() == imageid){
//                iter.remove();
//            }
//        }
        int i=0;
        for (image container:opList){
            if(Objects.equals(container.getImageid(), imageid))
            {
                break;
            }
            else{
                i++;
            }
        }
        opList.remove(i);
        return "success";
        /********** 测 试  ************/
    }
}